package com.tcse.ppmethodhandler.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tcse.ppmethodhandler.entity.HttpTrace;
import com.tcse.ppmethodhandler.entity.SpanRecord;
import com.tcse.ppmethodhandler.response.Rets;
import com.tcse.ppmethodhandler.service.CollectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jackson.JsonComponent;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping(value = "/collect", produces = "application/json; charset=utf-8")
public class CollectController {
    private static final ObjectMapper objectMapper = new ObjectMapper();
    
    @Autowired
    private CollectService collectService;
    
    @PostMapping("/span")
    public Object collect(@RequestBody String data) throws JsonProcessingException {
        // log.debug("Received Trace Data :  " + data);
    
        HttpTrace trace = objectMapper.readValue(data, HttpTrace.class);
        collectService.addTrace(trace);
        
        return Rets.success("成功");
    }
    
    @PostMapping("/finish")
    public Object finish(@RequestBody String task) {
        
        // 删除任务信息
        CollectService.sortSpan();
        log.debug(String.valueOf(CollectService.testSpanIdTraceMap));
        CollectService.writeFile();
        return Rets.success();
    }
}

